// pages/index/blue_tip/blue_tip.js
//常量数据配置
var ConstantData = require('../../../common/constant_data.js')
var commonUtil = require('../../../utils/commonUtil');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    checkboxStatus: 'checkboxNoSelect',
    checkboxImg: "checkboxNoSelect.png",
    tipStatus: false,
    phoneType: "vivo",
    tipsText1: '按上面的图示打开系统定位之后',
    tipsText2: '请勾选“确定已打开系统定位”',
    confirmText: '确定已打开系统定位',
    extraShow: false,
    miniImg: {
      blueFailLocation_huaweiA: ConstantData.CONSTATNT_DATA.BASE_URL_IMG + '/mini/blueFailLocation_huaweiA.png',
      blueFailLocation_huaweiB: ConstantData.CONSTATNT_DATA.BASE_URL_IMG + '/mini/blueFailLocation_huaweiB.png',
      blueFailLocation_huaweiC: ConstantData.CONSTATNT_DATA.BASE_URL_IMG + '/mini/blueFailLocation_huaweiC.png',
      blueFailLocation_meizuA: ConstantData.CONSTATNT_DATA.BASE_URL_IMG + '/mini/blueFailLocation_meizuA.png',
      blueFailLocation_meizuB: ConstantData.CONSTATNT_DATA.BASE_URL_IMG + '/mini/blueFailLocation_meizuB.png',
      blueFailLocation_oppoA: ConstantData.CONSTATNT_DATA.BASE_URL_IMG + '/mini/blueFailLocation_oppoA.png',
      blueFailLocation_oppoB: ConstantData.CONSTATNT_DATA.BASE_URL_IMG + '/mini/blueFailLocation_oppoB.png',
      blueFailLocation_oppoC: ConstantData.CONSTATNT_DATA.BASE_URL_IMG + '/mini/blueFailLocation_oppoC.png',
      blueFailLocation_oppoD: ConstantData.CONSTATNT_DATA.BASE_URL_IMG + '/mini/blueFailLocation_oppoD.png',
      blueFailLocationA_vivo: ConstantData.CONSTATNT_DATA.BASE_URL_IMG + '/mini/blueFailLocationA_vivo.jpg',
      huawei: ConstantData.CONSTATNT_DATA.BASE_URL_IMG + '/mini/wx_ble_location_huawei.png',
      vivo: ConstantData.CONSTATNT_DATA.BASE_URL_IMG + '/mini/wx_ble_location_vivo.png',
      iphone: ConstantData.CONSTATNT_DATA.BASE_URL_IMG + '/mini/wx_ble_location_iphone.png',
      ios_high: ConstantData.CONSTATNT_DATA.BASE_URL_IMG + '/mini/wx_ble_auth_ios.png',
      ios_low: ConstantData.CONSTATNT_DATA.BASE_URL_IMG + '/mini/wx_ble_auth_ios_low.png',
      oppo: ConstantData.CONSTATNT_DATA.BASE_URL_IMG + '/mini/wx_ble_location_oppo.png',
      xiaomi: ConstantData.CONSTATNT_DATA.BASE_URL_IMG + '/mini/wx_ble_location_xiaomi.png',
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var phoneType = options.phoneType;
    var type = options.type;
    var system = options.system;
    console.log('options:', options);
    if (!phoneType) {
      phoneType = "vivo"
    }
    this.setData({
      imgSrc: this.getImgSrc(phoneType, type, system),
      type: type
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getImgSrc(phoneType, type, system) {
    if (type == 'ble') {
      this.setData({
        tipsText1: '按上面的图示打开微信蓝牙权限之后',
        tipsText2: '请勾选“确定已打开蓝牙权限”',
        confirmText: '确定已打开蓝牙权限',
      })
      if (system.indexOf('ios 13') != -1 || system.indexOf('ios 14') != -1) {
        return this.data.miniImg.ios_high;
      } else {
        return this.data.miniImg.ios_low;
      }
    } else if (phoneType.indexOf('huawei') != -1) {
      return this.data.miniImg.huawei;
    } else if (phoneType.indexOf('oppo') != -1) {
      return this.data.miniImg.oppo;
    } else if (phoneType.indexOf('xiaomi') != -1) {
      return this.data.miniImg.xiaomi;
    } else if (phoneType.indexOf('iphone') != -1) {
      return this.data.miniImg.iphone;
    } else {
      return this.data.miniImg.vivo;
    }
  },
  clickCheckbox: function () {

    if (this.data.checkboxStatus == 'checkboxSelected') {
      this.setData({
        checkboxStatus: 'checkboxNoSelect',
        checkboxImg: "checkboxNoSelect.png",
        continueLeaseStat: false
      })
    } else {
      var that = this;
      if (that.data.type == 'ble') {
        wx.openBluetoothAdapter({
          success(res) {
            that.setData({
              checkboxStatus: 'checkboxSelected',
              checkboxImg: "checkboxSelected.png",
              continueLeaseStat: true
            })
          },
          fail(res) {
            that.setData({
              extraShow: true,
              tipStatus: true,
              tipContent: "打开蓝牙和微信蓝牙权限后才能继续租赁",
              tipBtnName: "确 定"
            })
          }
        })
      } else {
        const sysSetting = commonUtil.getSysSetting()
        if (sysSetting != null) {
          if (sysSetting.locationEnabled) {
            that.setData({
              checkboxStatus: 'checkboxSelected',
              checkboxImg: "checkboxSelected.png",
              continueLeaseStat: true
            })
          } else {
            that.setData({
              extraShow: true,
              tipStatus: true,
              tipContent: "打开系统定位后才能继续租赁",
              tipBtnName: "确 定"
            })
          }
        } else {
          wx.getLocation({
            type: 'gcj02', //返回可以用于wx.openLocation的经纬度
            success: function (res) {
              that.setData({
                checkboxStatus: 'checkboxSelected',
                checkboxImg: "checkboxSelected.png",
                continueLeaseStat: true
              })
            },
            fail: function () {
              that.setData({
                extraShow: true,
                tipStatus: true,
                tipContent: "打开系统定位后才能继续租赁",
                tipBtnName: "确 定"
              })
            }
          })
        }
      }
    }
  },

  continueLease: function () {
    console.log("-----continueLease-->", this.data.continueLeaseStat);
    if (this.data.continueLeaseStat) {
      //跳转到首页，重新扫码
      wx.redirectTo({
        url: '../index?pt=blue_tip',
      })
    } else {
      this.setData({
        extraShow: true,
        tipStatus: true,
        tipContent: "请勾选确定已打开系统定位",
        tipBtnName: "确 定"
      })
    }
  },

  tipBtnClick: function () {
    this.setData({
      tipStatus: false
    })
  }

})